package demo.practice.dp;

public class P209 {

    public int minSubArrayLen(int target, int[] nums) {

        int store[] = new int[nums.length + 1];
        for (int i = 1; i < store.length; i++) {
            store[i] = store[i - 1] + nums[i - 1];
            if (nums[i - 1] == target) {
                return 1;
            }
        }

        int i = 0;
        int j = 1;

        int res = 0;
        while (i < j && j < store.length) {
            if (store[j] - store[i] >= target) {
                res = res == 0 ? j - i : Math.min(res, j - i);
                i++;
            } else {
                j++;
            }
            if (res == 1)
                return 1;
        }

        return res;
    }

}
